home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part2 / 13181 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  4.5 KB

  1. Path: rcp6.elan.af.mil!rscernix!danpop
  2. From: danpop@mail.cern.ch (Dan Pop)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Does C convert float to double internally ?
  5. Date: 4 Apr 96 19:30:41 GMT
  6. Organization: CERN European Lab for Particle Physics
  7. Message-ID: <danpop.828646241@rscernix>
  8. References: <4jsllh$hkf@harbinger.cc.monash.edu.au> <4jv2ho$r1o@harbinger.cc.monash.edu.au>
  9. NNTP-Posting-Host: ues5.cern.ch
  10. X-Newsreader: NN version 6.5.0 #7 (NOV)
  11.  
  12. In <4jv2ho$r1o@harbinger.cc.monash.edu.au> bcheung@yoyo.cc.monash.edu.au (Biggles Cheung) writes:
  13.  
  14. >Biggles Cheung (bcheung@yoyo.cc.monash.edu.au) wrote:
  15. >: I was told that C compiler automatically converts "float" variable
  16. >: internally to "double" for + - * / , etc operations. Is this true?
  17. >
  18. >After I have read some books on C/C++, they all say that K&R C used to
  19. >do _implicit_ conversion from float to double both in unary and binary
  20. >operations, while ANSI C has dropped the implicit conversion and happily
  21. >allows float to be float at all time.
  22.  
  23. The keyword here is "allows".  ANSI C doesn't _require_ an operation
  24. involving only float operands to be carried on in single precision.
  25.  
  26. >Can I safely assume now that ALL current C/C++ compliers are following ANSI C
  27. >standard?
  28.  
  29. Almost, but this doesn't solve your problem.  See above.
  30. >: 
  31. >: Can I force the C compiler not to do the conversion as I have an
  32. >: assignment on finding out various computing errors of different
  33. >: precisions? 
  34. >:
  35. >I have read also the Turbo C++ manual and it says "tcc -ff- " command
  36. >line option can force the command line compiler to follow STRICT ANSI C
  37. >rule on conversion, i.e. no implicit conversion.
  38.  
  39. There is no such rule in ANSI C (that I know of).  See above.
  40.  
  41. >Can I now be sure that if I use "tcc -ff- program.c", there is _no_
  42. >implicit conversion? But what about the IDE interface? 
  43.  
  44. Compiler specific questions do _not_ belong to this newsgroup.  We are
  45. discussing the language, not one compiler or another.  Try a platform
  46. specific newsgroup where that compiler is relevant (e.g. 
  47. comp.os.msdos.programmer).
  48.  
  49. >Can someone suggest a simple program to test if there is any implicit
  50. >conversion happening behind the curtain?
  51.  
  52. Such a program cannot be written in a portable way.  An implementation
  53. where float and double are identical is perfectly legal, as long as the
  54. minimal requirements for precision and range for the type double are met.
  55. >  
  56. >: My advice from the lecturer is to have temporary variable of
  57. >: float to split a long equation to smaller step so that I can maximize
  58. >: the impact of single precision number on the evaluation. Is this
  59. >: feasible?
  60. >
  61. >If I am to find the single precision value of say, 12.3457 to the power
  62. >of 13 by multiplying 12.3456 thirteen times, does that mean I have to
  63. >store the result of every _single_ multiplication to a temporary variable
  64. >of float to enforce the single precision? 
  65.  
  66. Yes.
  67.  
  68. >What if I have a very complicated mathematical expression like Taylor's
  69. >series plus sqrt plus more ... ? How many temporary variables I need and
  70. >when is the best time or where is the best spot to insert such
  71. >_nuisance_?
  72.  
  73. As many as needed.  The idea is to assign the result of _any_ operation
  74. to a float, to be sure that no calculation is done with more bits of
  75. precision than you actually want/need.
  76.  
  77. For most intents and purposes, there is no need to force the precision
  78. of the intermediary results to float.  The idea behind the current 
  79. definition of the language is to allow the implementor the flexibility
  80. to choose the most efficient way to perform the arithmetic on a given
  81. platform.  There are processors which have only double precision FPU's
  82. and forcing single precision for the intermediary results would mean
  83. a lot of conversions, which would waste precious cpu cycles.
  84.  
  85. >_/\/\/\/\/\___/\/\_________________________/\/\_________________________
  86. >_/\/\____/\/\__________/\/\/\/\___/\/\/\/\_/\/\_____/\/\/\_____/\/\/\/\_
  87. >_/\/\/\/\/\___/\/\___/\/\__/\/\_/\/\__/\/\_/\/\___/\/\/\/\/\_/\/\/\/\___
  88. >_/\/\____/\/\_/\/\_____/\/\/\/\___/\/\/\/\_/\/\___/\/\_____________/\/\_
  89. >_/\/\/\/\/\___/\/\/\_______/\/\_______/\/\_/\/\/\___/\/\/\/\_/\/\/\/\___
  90. >____________________/\/\/\/\___/\/\/\/\_________________________________
  91. >
  92. >    / biggles@c031.aone.net.au    \    
  93. >    \ bcheung@yoyo.cc.monash.edu.au    / ~~ Biggles CHEUNG
  94.  
  95. Please trim your signature and retain only the last two lines.  The others
  96. don't carry any bit of useful information and are a pure waste of
  97. bandwidth.
  98.  
  99. Dan
  100. --
  101. Dan Pop
  102. CERN, CN Division
  103. Email: danpop@mail.cern.ch 
  104. Mail:  CERN - PPE, Bat. 31 R-004, CH-1211 Geneve 23, Switzerland
  105.